﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S2_DataTable_UserManager
{
    public class UserManager
    {
        private static UserManager self = null;
        public static UserManager GetInstance()
        {
            if (self == null)
            {
                self = new UserManager();
            }
            return self;
        }
        private UserManager()
        {
            userTable = new DataTable("user");
            userTable.Columns.Add("id");
            userTable.Columns.Add("name");
            userTable.Columns.Add("gender");
            userTable.Columns.Add("age");
        }
        /// <summary>
        /// 显示主界面
        /// </summary>
        public void ShowMainUI()
        {
            //清屏
            Console.Clear();
            ShowTable();
            ShowControlPanel();
        }

        /// <summary>
        /// 展示数据表格
        /// </summary>
        public void ShowTable()
        {
            //展示用户信息列表
            Console.WriteLine("用户Id    姓名    性别     年龄");
            Console.WriteLine("----------------------------------");

            for (int i = 0; i < userTable.Rows.Count; i++)
            {
                DataRow row = userTable.Rows[i];
                string userId = row["id"].ToString();
                string userName = row["name"].ToString();
                string userGender = row["gender"].ToString();
                string userAge = row["age"].ToString();
                Console.WriteLine($"   {userId}      {userName}     {userGender}       {userAge}");
            }

            Console.WriteLine("----------------------------------");
        }
        /// <summary>
        /// 展示控制板
        /// </summary>
        public void ShowControlPanel()
        {
            //展示控制板
            Console.WriteLine("功能菜单：a:添加用户；e：编辑用户；d:删除用户");
            Console.Write("请输入菜单编号：");
            //接收用户输入的菜单编号
            optionCode = Console.ReadLine();
            SwitchOption();
        }
        /// <summary>
        /// 判断菜单编号
        /// </summary>
        public void SwitchOption()
        {
            //判断菜单编号
            switch (optionCode)
            {
                case "a":
                    AddUser();
                    break;
                case "e":
                    //编辑用户
                    EditUser();
                    break;
                case "d":
                    //删除用户
                    DeleteUser();
                    break;
                default:
                    Console.WriteLine("输入的菜单编号不存在");
                    BackMainUI();
                    break;
            }
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        public void AddUser()
        {
            //添加用户
            Console.WriteLine("当前操作-添加用户(a)");
            //提示输入用户信息
            Console.Write("请输入用户Id:");
            string userId = Console.ReadLine();

            Console.Write("请输入用户姓名:");
            string userName = Console.ReadLine();

            Console.Write("请输入用户性别:");
            string userGender = Console.ReadLine();

            Console.Write("请输入用户年龄:");
            string userAge = Console.ReadLine();

            bool isHas = false;
            for (int i = 0; i < userTable.Rows.Count; i++)
            {
                DataRow row = userTable.Rows[i];
                if (row["id"].ToString() == userId)
                {
                    isHas = true;
                    break;
                }
            }
            if (isHas)
            {
                Console.WriteLine("保存失败");
            }
            else
            {
                userTable.Rows.Add(userId, userName, userGender, userAge);
                Console.WriteLine("保存成功");
            }
            BackMainUI();
        }
        /// <summary>
        /// 编辑用户
        /// </summary>
        public void EditUser()
        {
            // 编辑用户
            Console.WriteLine("当前操作-编辑用户(e)");
            //提示输入用户Id 从userTable中查找用户信息，然后进行编辑
            Console.Write("请输入用户Id:");
            string userId = Console.ReadLine();

            Console.Write("请输入用户姓名:");
            string userName = Console.ReadLine();

            Console.Write("请输入用户性别:");
            string userGender = Console.ReadLine();

            Console.Write("请输入用户年龄:");
            string userAge = Console.ReadLine();

            bool isSave = false;
            for (int i = 0; i < userTable.Rows.Count; i++)
            {
                DataRow row = userTable.Rows[i];
                if (row["id"].ToString() == userId)
                {
                    row["name"] = userName;
                    row["gender"] = userGender;
                    row["age"] = userAge;
                    isSave = true;
                    break;
                }
            }
            if (isSave)
            {
                Console.WriteLine("更新成功");
            }
            else
            {
                Console.WriteLine("更新失败");
            }

            BackMainUI();
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public void DeleteUser()
        {
            // 删除用户
            Console.WriteLine("当前操作-删除用户(d)");
            //提示输入用户Id 从userTable中查找用户信息，然后进行删除
            Console.Write("请输入用户Id:");
            string userId = Console.ReadLine();


            bool isDel = false;
            for (int i = 0; i < userTable.Rows.Count; i++)
            {
                DataRow row = userTable.Rows[i];
                if (row["id"].ToString() == userId)
                {
                    row.Delete();
                    isDel = true;
                    break;
                }
            }
            if (isDel)
            {
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("删除失败");
            }

            BackMainUI();
        }

        public void BackMainUI()
        {
            Console.WriteLine("按任意键返回");
            Console.ReadKey();
            ShowMainUI();
        }

        string optionCode = string.Empty;
        DataTable userTable;
    }
}
